﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using LMKJ.Classes;

namespace LMKJ
{
    public partial class edititem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string item_name = Request.QueryString["details"];

                Item i = DatabaseManager.RetrieveItemByName(item_name);

                if (i != null)
                {
                    itemNameLbl.Text = i.Item_name;
                    descriptionTbx.Text = i.Description;
                    string[] price = i.Price.ToString().Split('.');
                    dollarsTbx.Text = price[0];
                    centsTbx.Text = price[1];
                }
            }
        }

        protected void confirmBtn_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Item updatedItem = new Item();
                updatedItem.Description = descriptionTbx.Text;
                updatedItem.Price = Convert.ToDouble(dollarsTbx.Text + "." + centsTbx.Text);
                updatedItem.Item_name = itemNameLbl.Text;

                int success = DatabaseManager.UpdateItem(updatedItem);

                if (success == 1)
                {
                    string jsPopup = "<script type='text/javascript'>" +
                            "alert('Successfully Updated Item!');" +
                            "window.location.href = 'viewitemdetails.aspx?details=" +
                            Request.QueryString["details"] +
                            "';" +
                            "</script>";
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", jsPopup);
                }
            }
        }

        protected void cancelBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("viewitemdetails.aspx?details=" + Request.QueryString["details"]);
        }

        protected void deleteBtn_Click(object sender, EventArgs e)
        {
            confirmationLbl.Visible = true;
            confirmationBtn.Visible = true;
        }

        protected void fieldsValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (String.IsNullOrEmpty(dollarsTbx.Text.Trim()))
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }

        protected void priceValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            Regex NumberOnly = new Regex("\\d+$");

            if (NumberOnly.IsMatch(dollarsTbx.Text) && NumberOnly.IsMatch(centsTbx.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        protected void confirmationBtn_Click(object sender, EventArgs e)
        {
            int success = DatabaseManager.DeleteItem(Request.QueryString["details"]);

            if (success == 1)
            {
                string jsPopup = "<script type='text/javascript'>" +
                        "alert('Successfully Deleted Item!');" +
                        "window.location.href = 'searchitems.aspx';" +
                        "</script>";
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", jsPopup);
            }
        }
    }
}